using Microsoft.Xna.Framework;

namespace GameStateManagement.View.Rendering
{
    public class AnimatedTexture //: Texture
    {
        private readonly double _timePerFrame;
        private double _totalElapsedTime;
        private readonly int _frameCount;
        private readonly int _frameWidth;
        private readonly int _frameHeight;
        private int _currentFrame;

        public AnimatedTexture(int frameWidth, int frameHeight, int frameCount, int timePerFrame)
        {
            _currentFrame = 0;
            _totalElapsedTime = 0;
            _frameWidth = frameWidth;
            _frameHeight = frameHeight;
            _frameCount = frameCount;
            _timePerFrame = timePerFrame;
        }

        public void Update(GameTime elapsedTime)
        {
            _totalElapsedTime += elapsedTime.ElapsedGameTime.TotalSeconds;
            if (_totalElapsedTime > _timePerFrame)
            {
                _currentFrame++;
                // Keep the Frame between 0 and the total frames, minus one.
                _currentFrame = _currentFrame % _frameCount;
                _totalElapsedTime -= _timePerFrame;
            }
        }

        public Rectangle GetCurrentFrameRectangle()
        {
            return new Rectangle(_currentFrame * _frameWidth, 0, _frameWidth, _frameHeight);
        }


    }
}
